﻿using Common.Core;
using Domain.Infrastructure.Redis;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;

namespace Domain.Infrastructure.ServiceExtensions
{
    /// <summary>
    /// Redis缓存 启动服务
    /// </summary>
    public static class RedisCacheSetup
    {
        public static IServiceCollection AddRedisCacheSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            //自我管理注册
            services.AddTransient<IRedisOperationRepository, RedisOperationRepository>();

            // 配置启动Redis服务，虽然可能影响项目启动速度，但是不能在运行的时候报错，所以是合理的
            services.AddSingleton<ConnectionMultiplexer>(sp =>
            {
                //获取连接字符串
                string redisConfiguration = ConfigurationUtil.App("RedisConfig", "ConnectionString");

                var configuration = ConfigurationOptions.Parse(redisConfiguration, true);

                configuration.ResolveDns = true;

                return ConnectionMultiplexer.Connect(configuration);
            });
            return services;
        }
    }
}
